package com.resource.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.resource.utils.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.resource.constant.OfficeApiConstants;
import com.resource.converter.CustomerConverter;
import com.resource.dto.CustomerSaveDto;
import com.resource.query.CustomerQuery;
import com.resource.request.CustomerSaveRequest;
import com.resource.response.CustomerResponse;
import com.resource.response.OfficeResponse;
import com.resource.service.CustomerService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 客户管理实现
 * 
 * @Author: xiongbo.wan
 * @Date: 19:21 2020/9/22 0022
 */
@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService {

	@Resource
	private RestTemplate restTemplate;
	@Resource
	private CustomerConverter customerConverter;
	@Value("${office.api.url}")
	private String apiUrl;
	@Value("${office.app.user}")
	private String user;

	@Override
	public void create(CustomerSaveDto dto)  {
		CustomerSaveRequest customerSaveRequest = customerConverter.dtoToRequest(dto);
		String uri = String.format(OfficeApiConstants.CUSTOMER_SAVE_URL, apiUrl);
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.POST, JSONUtil.toJsonStr(customerSaveRequest),user);
		log.info("【customer create request】{}", JSONUtil.toJsonStr(customerSaveRequest));
		ResponseEntity<OfficeResponse> exchange = restTemplate.exchange(uri, HttpMethod.POST, entity,
				OfficeResponse.class);
		log.info("【customer create response!】{}", JSONUtil.toJsonStr(exchange));
		ValidateUtils.checkResponse(exchange);
	}

	@Override
	public void remove(Long id) {
		String uri = String.format(OfficeApiConstants.CUSTOMER_INFO_URL, apiUrl, id);
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.DELETE, OfficeApiConstants.IS_NULL,user);
		log.info("【customer remove request】{}", JSONUtil.toJsonStr(entity));
		ResponseEntity<OfficeResponse> exchange = restTemplate.exchange(uri, HttpMethod.DELETE, entity,
				OfficeResponse.class);
		log.info("【customer remove response!】{}", JSONUtil.toJsonStr(exchange));
		ValidateUtils.checkResponse(exchange);
	}

	@Override
	public String modify(CustomerSaveDto dto, String id){
		CustomerSaveRequest customerSaveRequest = customerConverter.dtoToRequest(dto);
		String uri = String.format(OfficeApiConstants.CUSTOMER_INFO_URL, apiUrl, id);
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.PUT, JSONUtil.toJsonStr(customerSaveRequest),user);
		log.info("【customer modify request】{}", JSONUtil.toJsonStr(customerSaveRequest));
		ResponseEntity<OfficeResponse> exchange = restTemplate.exchange(uri, HttpMethod.PUT, entity,
				OfficeResponse.class);
		log.info("【customer modify response!】{}", JSONUtil.toJsonStr(exchange));
		ValidateUtils.checkResponse(exchange);
		return "修改成功！";
	}

	@Override
	public CustomerResponse load(Long id) {
		String uri = String.format(OfficeApiConstants.CUSTOMER_INFO_URL, apiUrl, id);
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.GET, OfficeApiConstants.IS_NULL,user);
		log.info("【customer detail request】{}", JSONUtil.toJsonStr(entity));
		ResponseEntity<OfficeResponse> exchange = restTemplate.exchange(uri, HttpMethod.GET, entity,
				OfficeResponse.class);
		log.info("【customer detail response!】{}", JSONUtil.toJsonStr(exchange));
		OfficeResponse body = ValidateUtils.checkResponse(exchange);
		return BeanUtil.toBean(body.getData(), CustomerResponse.class);
	}

	@Override
	public List<CustomerResponse> list(CustomerQuery customerQuery) {
		Map<String, List<String>> condition;
		try {
			condition = SearchUtils.getQuery(customerQuery);
		} catch (Exception e) {
			throw new ServiceException("获取查询条件失败");
		}
		String uri = String.format(OfficeApiConstants.CUSTOMER_INFO_URL, apiUrl,
				String.format(OfficeApiConstants.IS_SEARCH, customerQuery.getPage(), customerQuery.getLimit()));
		Map<String, Object> params = new HashMap<>(2);
		params.put("search", JSONUtil.toJsonStr(condition));
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.GET, JSONUtil.toJsonStr(customerQuery),user);
		log.info("【customer list request】{}【url】{}", JSONUtil.toJsonStr(entity), uri);
		ResponseEntity<OfficeResponse> exchange = restTemplate.exchange(uri, HttpMethod.GET, entity,
				OfficeResponse.class, params);
		log.info("【customer list response】{}", JSONUtil.toJsonStr(exchange));
		OfficeResponse body = ValidateUtils.checkResponse(exchange);
		PageInfo<CustomerResponse> page = Convert.convert(new TypeReference<PageInfo<CustomerResponse>>() {
		}, body.getData());
		return page.getList();

	}

}
